package it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.storemenu.itemshop;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.ImageCache;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.storemenu.StorePanel;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.items.Item;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.pawn.stats.Stats;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

class ItemTreeCellRenderer extends JPanel implements TreeCellRenderer{
	
	private JLabel itemName = new JLabel();
	private JLabel itemPrice = new JLabel();
	
	private HashMap<String, String> settings;
	private ImageCache imgCache;
	
	
	private final static int SCALING = 16;
	
	ItemTreeCellRenderer(HashMap<String, String> settings, ImageCache imgCache) {
		this.settings = settings;
		this.imgCache = imgCache;
		this.setLayout(new BorderLayout(50,0));
		this.setPreferredSize(new Dimension(350,32));
		itemPrice.setHorizontalTextPosition(JLabel.LEFT);
		Image coinImg = imgCache.getImage(settings.get(StorePanel.COIN_IMAGE));
		itemPrice.setIcon(new ImageIcon(coinImg.getScaledInstance(coinImg.getWidth(null)/SCALING, coinImg.getWidth(null)/SCALING, Image.SCALE_SMOOTH)));
//		itemName.setHorizontalAlignment(JLabel.LEFT);
//		itemPrice.setHorizontalAlignment(JLabel.RIGHT);
		this.add(itemName, BorderLayout.LINE_START);
		this.add(itemPrice, BorderLayout.LINE_END);
	 	try {
	 		Font font = Font.createFont(Font.TRUETYPE_FONT, new File("cbvapps/uiresource/data/dupree.ttf"));
			itemName.setFont(font.deriveFont(Font.BOLD,20));
			itemPrice.setFont(font.deriveFont(Font.PLAIN, 20));
		} catch (FontFormatException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	   public Component getTreeCellRendererComponent(
               JTree tree,
               Object value,
               boolean sel,
               boolean expanded,
               boolean leaf,
               int row,
               boolean hasFocus) {
if(sel){
	this.setBackground(Color.decode("#91BEC1"));
	this.setOpaque(true);
}
else this.setOpaque(false);

if (value != null && value instanceof DefaultMutableTreeNode) {
	
     Object nodeObject =  ((DefaultMutableTreeNode) value).getUserObject();
   if(nodeObject instanceof Item){
	   Item item = (Item) nodeObject;
 	itemName.setText(item.getName());
 	itemName.setIcon(null);
	itemPrice.setVisible(true);
 	itemPrice.setText(Integer.toString((int) item.getBuyPrice()));
 //  setIcon(tutorialIcon);
   setToolTipText(null);
   this.setEnabled(tree.isEnabled());
   } else if(nodeObject instanceof Integer) {
		int stat = (int) nodeObject;
		itemPrice.setVisible(false);
		itemName.setText(Stats.statsKeyString(stat));
		itemName.setIcon(new ImageIcon(imgCache.getImage(settings.get(StorePanel.STATS_ICON_PATH)+stat+settings.get(StorePanel.STATS_ICON_EXTENSION))));
		}
 }
return this;

}
	
}